package cn.wsalix.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.utils.BeanMapper;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.msg.entity.Message;
import cn.wsalix.msg.form.MessageForm;
import cn.wsalix.observable.service.PayService;
import cn.wsalix.observable.service.SendService;
import cn.wsalix.order.entity.Order;
import cn.wsalix.order.form.WeiXinNotifyForm;
import cn.wsalix.order.service.OrderService;
import cn.wsalix.shop.service.ProductService;
import cn.wsalix.type.MessTypeEnum;
import cn.wsalix.user.entity.Mine;
import cn.wsalix.user.form.MessRapportForm;
import cn.wsalix.user.form.MineForm;
import cn.wsalix.user.repository.MineRepository;
import cn.wsalix.user.service.MineService;

@Service("mineService")
public class MineServiceImpl extends
		SimpleServiceImpl<MineForm, MineRepository, Mine>
		implements MineService {
	@Autowired
	private MineRepository myProductRepository;
	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductService productService;

	@Override
	public String module() {
		return "/admin/my_product";
	}

	@Override
	public MineRepository getRepository() {
		return myProductRepository;
	}

	@Transactional
	@Override
	public Mine add(MineForm form) {
		List<Mine> myProducts = findByForm(form);
		if (myProducts == null || myProducts.isEmpty()) {
			Mine obj = BeanMapper.map(form, Mine.class);
			/*Product product = productService.findById(form.getProductId());
			if (form.getProductId() != null && form.getProductId() != 0) {
				// obj.setProduct(product);
				obj.setProductId(form.getProductId());
			}*/
			if (form.getUserId() != null && form.getUserId() != 0) {
				obj.setUser(userService.findById(form.getUserId()));
			}
/*			if (form.isTuan()) {
				obj.setTuan(true);
			}
			obj.setName(product.getName());*/
			/*
			 * 商品附加信息
			 * obj.setLogo(product.getLogo());
			obj.setOriginLogo(product.getOriginLogo());
			obj.setStarNum(50);*/

			// obj.setMsgNum(0);

			return super.add(obj);
		} else {
			Mine obj = myProducts.get(0);
			// obj.setOrder(form.getOrder());
			//obj.setOrderTime(form.getOrderTime());
			//obj.setOrderDate(form.getOrderDate());
			obj = super.edit(obj);
			return obj;
		}

	}

	@Override
	public Class<Mine> getEntityClazz() {
		return Mine.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(MineForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		/*
		 * if (form.getProductId() != null && form.getProductId() != 0) {
		 * filters.put("product.id", new SearchFilter("product.id", Operator.EQ,
		 * form.getProductId())); }
		 */
		if (form.getName() != null && !form.getName().trim().equals("")) {
			filters.put("name",
					new SearchFilter("name", Operator.LIKE, form.getName()));
		}
		if (form.getUserId() != null && form.getUserId() != 0) {
			filters.put("user.id", new SearchFilter("user.id", Operator.EQ,
					form.getUserId()));
		}
		if (form.getFkId() != null && form.getFkId() != 0) {
			filters.put("fkId", new SearchFilter("fkId", Operator.EQ,
					form.getFkId()));
		}
/*		if (form.isTuanQuery()) {
			filters.put("tuan",
					new SearchFilter("tuan", Operator.EQ, form.isTuan()));
		}
*/
		return filters;
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof PayService) {
			PayService payService = (PayService) o;
			if (arg instanceof WeiXinNotifyForm) {
				WeiXinNotifyForm form = (WeiXinNotifyForm) arg;
				Order order = payService.getOrder();
				if (payService.isTreate()) {
					MineForm mineForm = new MineForm();
					mineForm.setUserId(order.getCreatedBy());// orderForm.getUserId());
					mineForm.setFkId(order.getProduct().getId());// orderForm.getProductId());
					/*if (order.getOrderType().ordinal() == OrderTypeEnum.tuan
							.ordinal()) {
						mineForm.setTuan(true);
					}
					//
					mineForm.setOrder(order);
					mineForm.setOrderId(order.getId());
					mineForm.setProductId(order.getProduct().getId());
					mineForm.setOrderTime(order.getOrderTime());
					mineForm.setOrderDate(order.getOrderDate());*/
					add(mineForm);
				}
			}
		}
		if (o instanceof SendService) {
			SendService sendService = (SendService) o;
			if (arg instanceof MessageForm) {
				MessageForm form = (MessageForm) arg;
				Message message = sendService.getMessage();
				if (message.getType().ordinal() == MessTypeEnum.product
						.ordinal()) {
					List<Mine> resLst = findByProductId(message.getFkId());
					for (Mine myProduct : resLst) {
						myProduct.setMsgNum(myProduct.getMsgNum() + 1);
						super.edit(myProduct);
					}
				}
			}
		}
	}

	@Override
	public List<Mine> findByForm(MineForm form) {
		// 根据用户获取我的商品，清空当前用户消息数
		return super.findByForm(form);
	}

	@Override
	public List<Mine> findByProductId(Long productId) {
		MineForm myProductForm = new MineForm();
		myProductForm.setFkId(productId);
		return findByForm(myProductForm);
	}

	@Override
	public boolean sendMess(MessRapportForm form) {
		// TODO 修改未读消息数
		return true;
	}

	/*
	 * @Override public void update(Observable o, Object arg) { if (o instanceof
	 * ProductChangeService) { ProductForm form = (ProductForm) arg; BrandForm
	 * brandForm = new BrandForm(); brandForm.setProductId(form.getId());
	 * brandForm.setBrand(form.getBrand());
	 * brandForm.setBrandLogo(form.getBrandLogo()); edit(brandForm); } }
	 */
}
